import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export const SET_USER_DATA = 'SET_USER_DATA'
let modules = {}
let requireContext = require.context('./', false, /\.store.js$/)
requireContext.keys().forEach(path => {
    let apiObject = requireContext(path).default
    let key = path.match(/\.\/(\w+).store/)[1]
    modules[key] = apiObject
});

export default new Vuex.Store({
    state:{
        userData:JSON.parse(sessionStorage.getItem('userData'))
    },
    mutations: {
        [SET_USER_DATA](state, userData){
            state.userData = userData
        }
    },
    actions:{
    	[SET_USER_DATA]({commit},userData){
    		sessionStorage.setItem('userData', JSON.stringify(userData));
    		commit(SET_USER_DATA,userData);
        },
        LOGOUT({commit},menus){
            sessionStorage.removeItem('userData');
            sessionStorage.removeItem('User-Menus');
    		commit(SET_USER_DATA, []);
        }
    },
    getters:{
       userData: state => state.userData
    },
    modules
})